package com.jichangxiu.common.entity.bo;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * SecurityUser:
 *
 * @author Jcx
 * @create 2023-04-03 21:49
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "系统安全用户")
public class SecurityUser implements UserDetails {

    /**
     * 用户对象
     */
    @Schema(description = "系统用户对象")
    private Record record;

    @Schema(description = "系统租户对象")
    private Record tenant;

    @Schema(description = "系统安全角色")
    private List<SecurityRole> securityRoles;

    @Schema(description = "系统安全权限")
    private List<SecurityPower> securityPowers;

    public String getUserId() {
        return record.getStr("userId");
    }

    public String getUserCode() {
        return record.getStr("userCode");
    }

    public String getDeptId() {
        return record.getStr("deptId");
    }

    public String getTenantId() {
        return record.getStr("tenantId");
    }

    public boolean getIsPlatform() {
        return record.getBoolean("isPlatform");
    }

    public List<String> getAuthList() {
        List<String> authList = record.getList("permissions", String.class);
        if (ObjectUtil.isEmpty(authList)) {
            authList = new ArrayList<>();
        }
        if (ObjectUtil.isNotEmpty(this.securityRoles)) {
            for (SecurityRole securityRole : this.securityRoles) {
                authList.add(securityRole.getAuthority());
            }
        }
        if (ObjectUtil.isNotEmpty(this.securityPowers)) {
            for (SecurityPower securityPower : this.securityPowers) {
                authList.add(securityPower.getAuthority());
            }
        }
        return authList;
    }

    public List<String> getPermissions() {
        List<String> permissions = record.getList("permissions", String.class);
        if (ObjectUtil.isEmpty(permissions)) {
            permissions = new ArrayList<>();
        }
        return permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        List<String> permissions = record.getList("permissions", String.class);
        if (ObjectUtil.isNotEmpty(permissions)) {
            permissions.forEach(permission -> {
                grantedAuthorities.add(new GrantedAuthority() {
                    @Override
                    public String getAuthority() {
                        return permission;
                    }
                });
            });
        }

        if (ObjectUtil.isNotEmpty(this.securityRoles)) {
            grantedAuthorities.addAll(this.securityRoles);
        }

        if (ObjectUtil.isNotEmpty(this.securityPowers)) {
            grantedAuthorities.addAll(this.securityPowers);
        }
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return record.getStr("password");
    }

    @Override
    public String getUsername() {
        return record.getStr("username");
    }

    // 该方法返回一个布尔值，用于指示用户账号是否过期。如果用户账号已过期，则返回 false，否则返回 true
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 用于指示用户账号是否被锁定。如果用户账号已被锁定，则返回 false，否则返回 true
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 用于指示用户凭证是否过期。如果用户凭证已过期，则返回 false，否则返回 true
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return record.getBoolean("status");
    }

}
